package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.operationmanual;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.MineRadioAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.SpaceCourseDataBean;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.view.dialog.ComTwoBtnDialog;

import java.util.ArrayList;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.OnClick;

/**
 * 优惠券核销
 */
public class OperationManualFragment extends BaseFragment {
    public static OperationManualFragment newInstance(String uuid) {
        OperationManualFragment operationManualFragment = new OperationManualFragment();
        Bundle bundle = new Bundle();
        bundle.putString("uuid", uuid);
        operationManualFragment.setArguments(bundle);
        return operationManualFragment;
    }

    @BindView(R.id.fl_view)
    FrameLayout fragView;
    @BindView(R.id.no_data_view)
    View mNoDataLayout;
    @BindView(R.id.tv_select_num)
    TextView mTvSelectNum;
    @BindView(R.id.btn_delete)
    TextView mBtnDelete;
    @BindView(R.id.select_all)
    TextView mSelectAll;
    @BindView(R.id.recycler_list)
    RecyclerView recyclerView;
    @BindView(R.id.check_box)
    ImageView check_box;

    @BindView(R.id.ll_mycollection_bottom_dialog)
    LinearLayout mLlMycollectionBottomDialog;
    private MineRadioAdapter mineRadioAdapter;
    private ArrayList<SpaceCourseDataBean> beanList;
    private OperationManualMode manualMode;
    private int index = 0;
    private boolean isSelectAll = false;
    private ComTwoBtnDialog comTwnBtnDialog;
    private static final int MYLIVE_MODE_CHECK = 0;
    private static final int MYLIVE_MODE_EDIT = 1;
    private int mEditMode = MYLIVE_MODE_CHECK;
    private boolean editorStatus = true;

    @Override
    protected int initLayout() {
        return R.layout.fg_operationmanual;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.CouponWriteOff), getActivity());
    }

    @Override
    protected void initData(Context mContext) {
        manualMode = getViewModelProvider().get(OperationManualMode.class);
        beanList = new ArrayList<>();
        mineRadioAdapter = new MineRadioAdapter(getContext(), beanList, 0);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mineRadioAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                if (beanList.size() == 0) {
                    fragView.setVisibility(View.GONE);
                    mNoDataLayout.setVisibility(View.VISIBLE);
                    mLlMycollectionBottomDialog.setVisibility(View.GONE);
                } else {
                    fragView.setVisibility(View.VISIBLE);
                    mNoDataLayout.setVisibility(View.GONE);
                    mLlMycollectionBottomDialog.setVisibility(View.VISIBLE);
                }
            }
        });
        mineRadioAdapter.setOnItemClickListener((v, position) -> {
            if (editorStatus) {
                SpaceCourseDataBean myLive = mineRadioAdapter.getListData().get(position);
                boolean isSelect = myLive.isCheck();
                if (!isSelect) {
                    index++;
                    myLive.setCheck(true);
                } else {
                    myLive.setCheck(false);
                    index--;
                }
                if (index == mineRadioAdapter.getListData().size()) {
                    isSelectAll=true;
                    check_box.setImageResource(R.mipmap.hexiao_select);
                } else {
                    isSelectAll=false;
                    check_box.setImageResource(R.mipmap.hexiao_no);
                }

                //   setBtnBackground(index);
                mTvSelectNum.setText("张数：" + String.valueOf(index));
                mineRadioAdapter.notifyDataSetChanged();
            }
        });
        recyclerView.setAdapter(mineRadioAdapter);
        mineRadioAdapter.notifyDataSetChanged();
    }

    @Override
    protected void loadMethod() {
        getUUID();
        hexiaoDataResult();
    }

    @OnClick({R.id.btn_delete, R.id.select_all, R.id.selectAll})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_delete:
                deleteVideo();
                break;
            case R.id.select_all:
                // updataEditMode();
                break;
            case R.id.selectAll:
                if (!isSelectAll) {
                    isSelectAll = true;
                } else {
                    isSelectAll = false;
                }
                setSelectAll();
                break;
        }
    }

    /**
     * 调取核销接口处理结果
     */
    private void hexiaoDataResult() {
        manualMode.getLiveCodeData().observe(this, integer -> {
            if (integer == 200) {
                startWithPop(AddYHSufFragment.newInstance());
            }
        });
    }

    /**
     * 获取会员信息
     */
    private void getUUID() {
        manualMode.getLiveData().observe(this, spaceCourseDataBeans -> {
            beanList = spaceCourseDataBeans;
            mineRadioAdapter.setListData(beanList);
            mineRadioAdapter.notifyDataSetChanged();
        });
        manualMode.MemberByUuid(getActivity(), getArguments().getString("uuid"));
    }


    /**
     * 进行优惠券核销
     */
    private void deleteVideo() {
        if (index == 0) {
            ToastUtils.showToast(getContext(), "请至少选择一张要核销的优惠券~");
            return;
        }
        if (comTwnBtnDialog != null) {
            comTwnBtnDialog.dismiss();
        }
        comTwnBtnDialog = new ComTwoBtnDialog(getActivity(), ComTwoBtnDialog.DIALOG_CLEAR_MY_CACHE, null);
        comTwnBtnDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {
                comTwnBtnDialog.dismiss();
            }

            @Override
            public void onDialogRightBtnClick() {
                String idListStr = "";
                for (int i = mineRadioAdapter.getListData().size(), j = 0; i > j; i--) {
                    SpaceCourseDataBean myLive = mineRadioAdapter.getListData().get(i - 1);
                    if (myLive.isCheck()) {
                        mineRadioAdapter.getListData().remove(myLive);
                        index--;
                        idListStr = idListStr + "," + myLive.getId();
                    }
                }
                index = 0;
                mTvSelectNum.setText(String.valueOf(0));
                setBtnBackground(index);
                if (mineRadioAdapter.getListData().size() == 0) {
                    mLlMycollectionBottomDialog.setVisibility(View.GONE);
                }
                mineRadioAdapter.notifyDataSetChanged();
                if (!StringUtils.isEmpty(idListStr)) {
                    String substring = idListStr.substring(1, idListStr.length());
                    hexiaoData(substring);
                } else {
                    ToastUtils.showToast(getContext(), "请选择核销优惠券！");
                }

                comTwnBtnDialog.dismiss();
            }
        });
        comTwnBtnDialog.show();
    }

    /**
     * 调取核销接口
     */
    private void hexiaoData(String idListStr) {
        manualMode.getToken(getActivity(), idListStr);
    }


    /**
     * 根据选择的数量是否为0来判断按钮的是否可点击.
     *
     * @param size
     */
    private void setBtnBackground(int size) {
//        if (size != 0) {
//            mBtnDelete.setBackgroundResource(R.drawable.btn_jb);
//            mBtnDelete.setEnabled(true);
//            mBtnDelete.setTextColor(Color.WHITE);
//        } else {
//            mBtnDelete.setBackgroundResource(R.drawable.btn_jb);
//            mBtnDelete.setEnabled(false);
//            mBtnDelete.setTextColor(ContextCompat.getColor(getContext(), R.color.black));
//        }
    }

    /**
     * 更改编辑模式
     */
    private void updataEditMode() {
        mEditMode = mEditMode == MYLIVE_MODE_CHECK ? MYLIVE_MODE_EDIT : MYLIVE_MODE_CHECK;
        if (mEditMode == MYLIVE_MODE_EDIT) {
            mSelectAll.setText("取消");
            editorStatus = true;
            setItem();
        } else {
            mSelectAll.setText("选择优惠券");
            editorStatus = false;
            setItem();
        }
        mineRadioAdapter.setEditMode(mEditMode);
    }

    /**
     * 初始化选中模式
     */
    private void setItem() {
        for (int i = 0, j = mineRadioAdapter.getListData().size(); i < j; i++) {
            mineRadioAdapter.getListData().get(i).setCheck(false);
        }
        index = 0;
        mineRadioAdapter.notifyDataSetChanged();
        setBtnBackground(index);
        mTvSelectNum.setText(String.valueOf(index));
    }

    private void setSelectAll() {
        if (isSelectAll) {
            check_box.setImageResource(R.mipmap.hexiao_select);
            for (int i = 0, j = mineRadioAdapter.getListData().size(); i < j; i++) {
                mineRadioAdapter.getListData().get(i).setCheck(true);
            }
            index = mineRadioAdapter.getListData().size();
        } else {
            check_box.setImageResource(R.mipmap.hexiao_no);
            for (int i = 0, j = mineRadioAdapter.getListData().size(); i < j; i++) {
                mineRadioAdapter.getListData().get(i).setCheck(false);
            }
            index = 0;
        }
        mineRadioAdapter.notifyDataSetChanged();
        //  setBtnBackground(index);
        mTvSelectNum.setText("张数：" + String.valueOf(index));
    }
}
